import { defineStore } from 'pinia'
import { getSchoolLogos, getSchools, getSchoolDetail } from '@/api/school'

export const useSchoolStore = defineStore('school', () => {
  // 状态
  const schoolLogos = ref([])
  const schools = ref([])
  const currentSchool = ref(null)
  const loading = ref(false)

  // 获取院校Logo列表
  const fetchSchoolLogos = async () => {
    try {
      loading.value = true
      const { data } = await getSchoolLogos()
      schoolLogos.value = data || []
    } catch (error) {
      console.error('获取院校Logo失败:', error)
    } finally {
      loading.value = false
    }
  }

  // 获取院校列表
  const fetchSchools = async (params = {}) => {
    try {
      loading.value = true
      const { data } = await getSchools(params)
      schools.value = data?.list || []
      return data
    } catch (error) {
      console.error('获取院校列表失败:', error)
      return { list: [], total: 0 }
    } finally {
      loading.value = false
    }
  }

  // 获取院校详情
  const fetchSchoolDetail = async (id) => {
    try {
      loading.value = true
      const { data } = await getSchoolDetail(id)
      currentSchool.value = data
      return data
    } catch (error) {
      console.error('获取院校详情失败:', error)
      return null
    } finally {
      loading.value = false
    }
  }

  return {
    schoolLogos,
    schools,
    currentSchool,
    loading,
    fetchSchoolLogos,
    fetchSchools,
    fetchSchoolDetail
  }
})